package com.example.demo.common;


import com.p6spy.engine.spy.appender.FileLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogbackLogger extends FileLogger {
    private static final Logger logger = LoggerFactory.getLogger(LogbackLogger.class);

    public String getLastEntry() {
        return lastEntry;
    }

    public void setLastEntry(String lastEntry) {
        this.lastEntry = lastEntry;
    }

    protected String lastEntry;

    private String lastSql = "";

    public void logSQL(int connectionId, String s, long l, String category, String s1, String sql) {
        if ("result".equals(category)) {//过滤掉
            return; //result没有信息量。resultset才有
        }

        if ("resultset".equals(category)) {
            if (s1.equals(lastSql)) {
                return;//如果是重复的查询结果，即一个同一个查询多行结果导致的多行问题。 return
            } else {
                logger.info(s1);
                //logger.info(sql);
                lastSql = s1;
            }
        } else if ("statement".equals(category)) {
            logger.info(sql);
        } else if ("commit".equals(category)) {
            logger.info("commit");
        }

    }

    public void logException(Exception e) {
        logger.error(e.getMessage(), e);
    }

    public void logText(String s) {
        logger.info(s);
        this.setLastEntry(s);
    }

    private String trim(String sql) {
        StringBuilder sb = new StringBuilder("\r\n");
        sb.append(sql.replaceAll("\n|\r|\t|'  '", " "));
        return sb.toString();
    }
} 